<?php
// 应用公共文件

use app\common\service\AuthService;
use think\facade\Cache;
use app\api\common\controller\FileEveryDaySerialNumber;
use think\facade\Db;
use think\facade\Route;
if (!function_exists('sysconfig')) {

    /**
     * 获取系统配置信息
     * @param $group
     * @param null $name
     * @return array|mixed
     */
    function sysconfig($group, $name = null)
    {
        $where = ['group' => $group];
        $value = empty($name) ? Cache::get("sysconfig_{$group}") : Cache::get("sysconfig_{$group}_{$name}");
        if (empty($value)) {
            if (!empty($name)) {
                $where['name'] = $name;
                $value         = \app\admin\model\system\Config::where($where)->value('value');
                Cache::tag('sysconfig')->set("sysconfig_{$group}_{$name}", $value, 3600);
            } else {
                $value = \app\admin\model\system\Config::where($where)->column('value', 'name');
                Cache::tag('sysconfig')->set("sysconfig_{$group}", $value, 3600);
            }
        }
        return $value;
    }
}
if (!function_exists('__url')) {

    /**
     * 构建URL地址
     * @param string $url
     * @param array $vars
     * @param bool $suffix
     * @param bool $domain
     * @return string
     */
    function __url(string $url = '', array $vars = [], $suffix = true, $domain = false)
    {
        return url($url, $vars, $suffix, $domain)->build();
    }
}
//重写url 助手函数
if (!function_exists('__u')) {

    function __u($url = '', array $vars = [], $suffix = true, $domain = false)
    {
        $url = (string) Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
        return $url;
    }
}
if (!function_exists('password')) {

    /**
     * 密码加密算法
     * @param $value 需要加密的值
     * @param $type  加密类型，默认为md5 （md5, hash）
     * @return mixed
     */
    function password($value)
    {
        $value = sha1('blog_') . md5($value) . md5('_encrypt') . sha1($value);
        return sha1($value);
    }
}
if (!function_exists('xss_clean')) {
    /**
     * 清理XSS
     */
    function xss_clean($content, $is_image = false)
    {
        return \app\common\library\Security::instance()->xss_clean($content, $is_image);
    }
}
if (!function_exists('__')) {

    /**
     * 获取语言变量值
     * @param string $name 语言变量名
     * @param array  $vars 动态变量值
     * @param string $lang 语言
     * @return mixed
     */
    function __($name, $vars = [], $lang = '')
    {
        if (is_numeric($name) || !$name) {
            return $name;
        }
        if (!is_array($vars)) {
            $vars = func_get_args();
            array_shift($vars);
            $lang = '';
        }
        return \think\facade\Lang::get($name, $vars, $lang);
    }
}
if (!function_exists('del_image')) {
    /**
     * 图片删除 function
     *
     * @Author YJ 991850594@qq.com
     * @DateTime 2022-01-18
     * @param [type] $url
     * @return void
     */
    function del_image($url)
    {
        if (strpos($url, think\facade\Request::domain()) !== false) {
            // $new_url=str_replace(think\facade\Request::domain(),str_replace("..\\",'',ROOT_PATH),$url);
            $new_url = str_replace(think\facade\Request::domain(), str_replace("app", 'public', base_path()), $url);
            if (file_exists($new_url)) {
                @unlink($new_url);
            }
            return true;
        } else {
            return false;
        }
    }
}
if (!function_exists('auth')) {

    /**
     * auth权限验证
     * @param $node
     * @return bool
     * @throws \think\facade\db\exception\DataNotFoundException
     * @throws \think\facade\db\exception\DbException
     * @throws \think\facade\db\exception\ModelNotFoundException
     */
    function auth($node = null)
    {
        $authService = new AuthService(session('admin.id'));
        $check       = $authService->checkNode($node);
        return $check;
    }
}